package com.atguigu.tingshu.album.service.impl;


import com.atguigu.tingshu.album.mapper.BaseCategory1Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory2Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory3Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategoryViewMapper;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Override
	public List<JSONObject> getBaseCategoryList() {
		List<JSONObject> totalList = new ArrayList<>();
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
		Map<Long, List<BaseCategoryView>> category1List = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> category1Iterator = category1List.entrySet().iterator();
		while (category1Iterator.hasNext()){
			Map.Entry<Long, List<BaseCategoryView>> list1Entry = category1Iterator.next();
			Long key1 = list1Entry.getKey();
			List<BaseCategoryView> baseCategory1Views = list1Entry.getValue();
			JSONObject JSONObject1 = new JSONObject();
			JSONObject1.put("categoryId", key1);
			JSONObject1.put("categoryName", baseCategory1Views.get(0).getCategory1Name());
			List<JSONObject> viewList2 = new ArrayList<>();
			Map<Long, List<BaseCategoryView>> category2List = baseCategory1Views.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> category2Iterator = category2List.entrySet().iterator();
			while (category2Iterator.hasNext()){
				Map.Entry<Long, List<BaseCategoryView>> listEntry = category2Iterator.next();
				Long key2 = listEntry.getKey();
				List<BaseCategoryView> baseCategory2Views = listEntry.getValue();
				JSONObject JSONObject2 = new JSONObject();
				JSONObject2.put("categoryId",key2);
				JSONObject2.put("categoryName",baseCategory2Views.get(0).getCategory2Name());

				List<JSONObject> viewList3 = baseCategory2Views.stream().map(baseCategory2View -> {
					JSONObject JSONObject3 = new JSONObject();
					JSONObject3.put("categoryId", baseCategory2View.getCategory3Id());
					JSONObject3.put("categoryName", baseCategory2View.getCategory3Name());
					return JSONObject3;
				}).collect(Collectors.toList());

				JSONObject2.put("categoryChild",viewList3);
				viewList2.add(JSONObject2);
			}
			JSONObject1.put("categoryChild", viewList2);
			totalList.add(JSONObject1);
		}
		return totalList;
	}
}
